package com.zzrc.grape.controller;

import com.zzrc.grape.model.Role;
import com.zzrc.grape.model.User;
import com.zzrc.grape.service.MenuService;
import com.zzrc.grape.service.RoleService;
import com.zzrc.grape.service.UserService;
import com.zzrc.grape.util.BasePageData;
import com.zzrc.grape.util.StringUtil;
import com.zzrc.grape.util.UserUtils;
import com.zzrc.grape.util.WebResponseCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 角色管理Controller
 * @author Maochao-zhu
 *
 */
@Controller
@RequestMapping("/role")
public class RoleController {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Resource
	RoleService roleService;
	@Resource
	MenuService menuService;
	@Resource
	UserService userService;
	
	/**
	 * 去角色首页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("goIndex")
	 public ModelAndView goIndex(HttpServletRequest request,
                                 HttpServletResponse response){
		ModelAndView modelView = new ModelAndView();
       modelView.setViewName("views/role/index");
       return modelView; 
	}
	
	/**
	 * 去角色添加界面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("goAdd")
	 public ModelAndView goAdd(Role role, HttpServletRequest request,
							   HttpServletResponse response){
		ModelAndView modelView = new ModelAndView();
       modelView.setViewName("views/role/add");
    return modelView; 
	}
	
	/**
	 * 去编辑页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("goEdit")
	public ModelAndView goEdit(Role Role, Integer id, HttpServletRequest request,
                               HttpServletResponse response){
		ModelAndView modelView = new ModelAndView();
		modelView.addObject("zx_data", roleService.getRoleById(id));
		modelView.setViewName("views/role/edit");
		return modelView; 
	}
	
	
	/**
	 * 角色设定界面
	 * @param id
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("goRole")
	public ModelAndView goRole(Integer id, HttpServletRequest request,
                               HttpServletResponse response){
		ModelAndView modelView = new ModelAndView();
		//角色信息
		Role role = roleService.getRoleById(id);

		//用于页面空指针判断
		if(StringUtil.isBlank(role.getMenuControl())){
			role.setMenuControl("-");
		}
		if(StringUtil.isBlank(role.getOperControl())){
			role.setOperControl("-");
		}
		modelView.addObject("zx_data",role);
		//菜单信息
		modelView.addObject("menuList", menuService.getMenuListAll());
		modelView.setViewName("views/role/role");
		return modelView; 
	}
	
	@RequestMapping("goRoleData")
	@ResponseBody
	 public BasePageData goRoleData(Role role, HttpServletRequest request,
									HttpServletResponse response){
		BasePageData data = new BasePageData();
		data.setCode(WebResponseCode.SUCCESS);
		data.setMsg("操作成功!");
		data.setData(menuService.getMenuListAll());
		return data;
	}
	
	/**
	 * 获取数据列表信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("getList")
	@ResponseBody
	 public BasePageData getList(Role role,HttpServletRequest request,
	            HttpServletResponse response){
		BasePageData data = new BasePageData();
		List<Role> list = roleService.getRoleList(role);
		Integer count = roleService.getRoleListCount(role);
		data.setCode(WebResponseCode.SUCCESS);
		data.setMsg("操作成功!");
		data.setCount(count); 
		data.setData(list);
		return data;
	}

	
	/**
	 * 根据ID获取角色信息
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("get")
	@ResponseBody
	public BasePageData get(Integer id,HttpServletRequest request,
			HttpServletResponse response){
		BasePageData data = new BasePageData();
		Role Role = roleService.getRoleById(id);
		data.setData(Role);
		data.setCode(WebResponseCode.SUCCESS);
		data.setMsg("操作成功!");
		return data;
	}
}
